<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.User"%>

<script type="text/javascript">
<!--
	$(document).ready(function() {		
		$("#cancelEditRealAccountBtn").click(function() {
			$("#editRealAccountDiv").dialog("close");
		});
		
		$("[id^='doAjaxSaveRealAccountBtn-']").click(function() {
			var trId = this.id.substring(25);
			doSaveRealAccountAction(trId);			
		});
		
		$("[id^='doAjaxDeleteRealAccountBtn-']").click(function() {
			var trId = this.id.substring(27);
			doDeleteRealAccountAction(trId);			
		});
		
		$("#addEmptyRealAccountBtn").click(function() {
			addTableRow( $('.realAccountList'));
		});
		
	});

	function doSaveRealAccountAction(trId)
	{
		var realAccountPkId = '#realAccount\\.realAccountPk-' + trId;
		var realAccountPk = $(realAccountPkId).val();
		
		var virtualAccountNoId = '#realAccount\\.virtualAccountNo-' + trId;
		var virtualAccountNo = $(virtualAccountNoId).val();
		
		var accountNoId = '#realAccount\\.accountNo-' + trId;
		var accountNo = $(accountNoId).val();
		
		var partnerNoId = '#realAccount\\.partnerNo-' + trId;
		var partnerNo = $(partnerNoId).val();
		
		var groupNo = $('#editGroup\\.groupNo').val();
		groupNo=groupNo.substring(0,8);
	
		
		var errMsg = "";
		if (null == virtualAccountNo || 0 == virtualAccountNo.length){
			errMsg = errMsg + "虛擬帳號是必要欄位<br/>";
		}
		
		
		
		if (null == accountNo || 0 == accountNo.length){
			errMsg = errMsg + "實體帳戶是必要欄位<br/>";
		}
	
		if (null == partnerNo || 0 == partnerNo.length){
			errMsg = errMsg + "金鑽商家商家代碼是必要欄位<br/>";
		}
		if ( 3 > virtualAccountNo.length ||  4 < virtualAccountNo.length){
			errMsg = errMsg + "虛擬帳號長度為只能輸入3或4碼<br/>";
		}
	
		if (null == accountNo || 14 != accountNo.length){
			errMsg = errMsg + "實體帳戶長度為14碼<br/>";
		}
		if (12 > partnerNo.length){
			errMsg = errMsg + "合作伙伴代碼請至少輸入12碼<br/>";
		}
		
		
		
		
		if (null != partnerNo){
			
			partnerNoChk=partnerNo.substring(0,8);
			if(partnerNoChk != groupNo)
			errMsg = errMsg + "合作伙伴代碼，前八碼需與業者代碼前八碼相同<br/>";
		}
		
	
		if (0 != errMsg.length){
			$("#ajaxErrorMessageDiv").html(errMsg);
			$("#ajaxErrorMessageDiv" ).dialog({  
				title:'請修正下列錯誤' 
				,width: 300
				, resizable: false
				, modal: true
				, buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});
			return false;
		}
		
		$('#hidden\\.editRealAccount\\.realAccountPk').val(realAccountPk);
		$('#hidden\\.editRealAccount\\.virtualAccountNo').val(virtualAccountNo);
		$('#hidden\\.editRealAccount\\.accountNo').val(accountNo);
		$('#hidden\\.editRealAccount\\.partnerNo').val(partnerNo);		
		$('#doAjaxMethodx').attr('name','doAjaxSaveRealAccount');
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9007.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editRealAccountHiddenForm").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#editRealAccountDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							if (1==$("#doAjaxSaveResultRealAccountCode").val()){								
								toAjaxEditRealAccountAction($("#hidden\\.editRealAccount\\.group\\.groupPk").val());
							}
							$(this).dialog("close");
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});
	}
	
	function doDeleteRealAccountAction(trId)
	{
		var realAccountPkId = '#realAccount\\.realAccountPk-' + trId;
		var realAccountPk = $(realAccountPkId).val();
		
		$('#hidden\\.editRealAccount\\.realAccountPk').val(realAccountPk);
		$('#doAjaxMethodx').attr('name','doAjaxDeleteRealAccount');
		
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9007.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editRealAccountHiddenForm").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#editRealAccountDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {								
							if (1==$("#doAjaxDeleteResultRealAccountCode").val()){
								toAjaxEditRealAccountAction($("#hidden\\.editRealAccount\\.group\\.groupPk").val());
							}
							$(this).dialog("close");
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});
	}
	
	function addTableRow(jQtable){

	    jQtable.each(function(){
	        var $table = $(this);
	        var newRowNum = $('tbody tr', this).length;
	        // Number of td's in the last table row
	        var n = $('tr:last td', this).length;
	        var tds = '<tr id="tr-'+newRowNum+'">';
	        	        
	        tds += '<td>';
	        tds += '<input id="realAccount.realAccountPk-'+newRowNum+'" name="realAccount.realAccountPk" value="" type="hidden">';
	        tds += '<input id="realAccount.virtualAccountNo-'+newRowNum+'" name="realAccount.virtualAccountNo" value="" maxlength="4" type="text">';
	        tds += '</td>';
	        
	        tds += '<td>';
	        tds += '<input id="realAccount.accountNo-'+newRowNum+'" name="realAccount.accountNo" value="" maxlength="14" type="text">';
	        tds += '</td>';
	        
	        tds += '<td>';
	        tds += '<input id="realAccount.partnerNo-'+newRowNum+'" name="realAccount.partnerNo" value="" maxlength="20" type="text">';
	        tds += '</td>';      

	        tds += '<td>';
	        tds += '<input id="doAjaxSaveRealAccountBtn-'+newRowNum+'" value="確認" type="button" onclick="doSaveRealAccountAction('+newRowNum+')" >&nbsp;';
	        tds += '<input id="doAjaxDeleteRealAccountBtn-'+newRowNum+'" value="刪除" type="button" onclick="doDeleteRealAccountAction('+newRowNum+')" disabled="disabled">';	        
	        tds += '</td>';
	        
	        tds += '</tr>';
	        
	        if($('tbody', this).length > 0){
	            $('tbody', this).append(tds);
	        }else {
	            $(this).append(tds);
	        }
	    });
	}
//-->
</script>
<div id="editRealAccountFormDiv">	
	<table id="listTable" class="realAccountList">
		<thead>
			<tr>
				<th>虛擬帳號</th>
				<th>實體帳戶</th>
				<th>金鑽商家 商家代碼</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach items="${actionBean.queryRealAccount.paginatedList}" var="viewObj" varStatus="rowstat">
			<tr id="tr-${rowstat.index}">
				<td>
					<input id="realAccount.realAccountPk-<c:out value='${rowstat.index}'/>" name="realAccount.realAccountPk" type="hidden" value="<c:out value="${viewObj.realAccountPk}"/>" />
					<input id="realAccount.virtualAccountNo-<c:out value='${rowstat.index}'/>" name="realAccount.virtualAccountNo" type="text" value="<c:out value="${viewObj.virtualAccountNo}"/>" maxlength="4" />
				</td>
				<td>
					<input id="realAccount.accountNo-<c:out value='${rowstat.index}'/>" name="realAccount.accountNo" type="text" value="<c:out value="${viewObj.accountNo}"/>" maxlength="14" />
				</td>
				<td>
					<input id="realAccount.partnerNo-<c:out value='${rowstat.index}'/>" name="realAccount.partnerNo" type="text" value="<c:out value="${viewObj.partnerNo}"/>" maxlength="20" />
				</td>				
				<td>
					<input type="button" value="確認" id="doAjaxSaveRealAccountBtn-<c:out value='${rowstat.index}'/>"/>&nbsp;<input type="button" value="刪除" id="doAjaxDeleteRealAccountBtn-<c:out value='${rowstat.index}'/>"/>
				</td>					
			</tr>
			</c:forEach>
		</tbody>
		<tfoot>			
			<tr>
				<td colspan="4" align="center">
					<input type="button" value="新增" id="addEmptyRealAccountBtn"/>
					<input type="button" value="離開" id="cancelEditRealAccountBtn"/>
				</td>				
			</tr>
		</tfoot>
	</table>
</div>

<div id="editRealAccountHiddenDiv" style="display: none;">
<stripes:form id="editRealAccountHiddenForm" name="editRealAccountHiddenForm" 
	beanclass="com.feib.soeasy.action.Soez9007ActionBean" method="post" onsubmit="javascript: return false;">
	<input id="hidden.editRealAccount.group.groupPk" name="editRealAccount.group.groupPk" value="${actionBean.editGroup.groupPk}"/>
	<input id="hidden.editRealAccount.realAccountPk" name="editRealAccount.realAccountPk" />
	<input id="hidden.editRealAccount.virtualAccountNo" name="editRealAccount.virtualAccountNo" />
	<input id="hidden.editRealAccount.accountNo" name="editRealAccount.accountNo" />
	<input id="hidden.editRealAccount.partnerNo" name="editRealAccount.partnerNo" />
	
	<input type="hidden" id="doAjaxMethodx" name="doAjaxSaveRealAccount" />
</stripes:form>
</div>

